# -*- coding: utf-8 -*-
import string
from docx import Document
from docx.shared import Pt, Inches
from docx.enum.style import WD_STYLE_TYPE

import arcpy
from ExportJPG import ExportToJPGFile

# 判断样式是否存在
def styleExists(document, style_name):
    stylename_list = [st.name for st in document.styles]
    if style_name in stylename_list:
        return True
    else:
        return False
# 添加样式到文档
def addStyleToDocument(document, style_name):
    if not styleExists(document,style_name):
        title_style = document.styles.add_style(style_name, WD_STYLE_TYPE.PARAGRAPH)
        title_style.font.size = Pt(14)
        title_style.font.bold=True
# 创建一个表格
def CreateTable(document,row, rsPicPath=None):
    table = document.add_table(rows=22,cols=25,style = 'Table Grid')
    # 室内编号
    t_roomCode1=table.cell(0,0)
    t_roomCode2=table.cell(0,4)
    t_roomCode1.merge(t_roomCode2)
    t_roomCode1.text=u"室内解译编号"

    c_roomCode1=table.cell(0,5)
    c_roomCode2=table.cell(0,11)
    c_roomCode1.merge(c_roomCode2)
    c_roomCode1.text=row.getValue(u"解译编号")

    # 地形图图幅号
    mapcode_1 = table.cell(0,12)
    mapcode_2 = table.cell(0,18)
    mapcode_1.merge(mapcode_2)
    mapcode_1.text=u"地形图图幅号"

    c_mapcode_1 = table.cell(0,19)
    c_mapcode_2 = table.cell(0,24)
    c_mapcode_1.merge(c_mapcode_2)
    c_mapcode_1.text=row.getValue(u"图框编号")

    # 自然地理位置
    t_pos_1=table.cell(1,0)
    t_pos_2=table.cell(1,4)
    t_pos_1.merge(t_pos_2)
    t_pos_1.text=u"自然地理位置"
    c_village_1 = table.cell(1,5)
    c_village_2 = table.cell(1,24)
    c_village_1.merge(c_village_2)
    c_village_1.text=row.getValue("Location")

    #矿企业名称
    t_miningName_1 = table.cell(2,0)
    t_miningName_2 = table.cell(2,4)
    t_miningName_1.merge(t_miningName_2)
    t_miningName_1.text=u"矿山企业名称"

    c_miningName_1 = table.cell(2,5)
    c_miningName_2 = table.cell(2,17)
    c_miningName_1.merge(c_miningName_2)
    c_miningName_1.text=row.getValue("Name")

    # 【填写待核查与否】
    c_waitVerify_1 = table.cell(2,18)
    c_waitVerify_2 = table.cell(2,24)
    c_waitVerify_1.merge(c_waitVerify_2)
    c_waitVerify_1.text=row.getValue(u"核查情况")

    #采矿方式
    t_miningMethod_1 = table.cell(3,0)
    t_miningMethod_2 = table.cell(3,4)
    t_miningMethod_1.merge(t_miningMethod_2)
    t_miningMethod_1.text=u"采矿方式"

    c_miningMethod_1 = table.cell(3,5)
    c_miningMethod_2 = table.cell(3,24)
    c_miningMethod_1.merge(c_miningMethod_2)
    c_miningMethod_1.text=row.getValue("Method")

    #坐标
    t_coordinateFact_1 = table.cell(4,0)
    t_coordinateFact_2 = table.cell(4,4)
    t_coordinateFact_1.merge(t_coordinateFact_2)
    t_coordinateFact_1.text=u"坐标"

    # Y
    t_coordinateFactY_1 = table.cell(4,5)
    t_coordinateFactY_2 = table.cell(4,9)
    t_coordinateFactY_1.merge(t_coordinateFactY_2)
    t_coordinateFactY_1.text=u"Y"

    c_coordinateFactY_1 = table.cell(4,10)
    c_coordinateFactY_2 = table.cell(4,14)
    c_coordinateFactY_1.merge(c_coordinateFactY_2)
    c_coordinateFactY_1.text="{0:.2f}".format(row.getValue(u"Y坐标"))

    # X
    t_coordinateFactX_1 = table.cell(4,15)
    t_coordinateFactX_2 = table.cell(4,19)
    t_coordinateFactX_1.merge(t_coordinateFactX_2)
    t_coordinateFactX_1.text=u"X"

    c_coordinateFactX_1 = table.cell(4,20)
    c_coordinateFactX_2 = table.cell(4,24)
    c_coordinateFactX_1.merge(c_coordinateFactX_2)
    c_coordinateFactX_1.text="{0:.2f}".format(row.getValue(u"X坐标"))

    #坐标第二行
    t_spaceCoordinate_1 = table.cell(5,0)
    t_spaceCoordinate_2 = table.cell(5,4)
    t_spaceCoordinate_1.merge(t_spaceCoordinate_2)
    # N
    t_coordinateFactN_1 = table.cell(5,5)
    t_coordinateFactN_2 = table.cell(5,9)
    t_coordinateFactN_1.merge(t_coordinateFactN_2)
    t_coordinateFactN_1.text=u"N"
    c_coordinateFactN_1 = table.cell(5,10)
    c_coordinateFactN_2 = table.cell(5,14)
    c_coordinateFactN_1.merge(c_coordinateFactN_2)
    c_coordinateFactN_1.text=row.getValue(u"纬度")

    # E
    t_coordinateFactE_1 = table.cell(5,15)
    t_coordinateFactE_2 = table.cell(5,19)
    t_coordinateFactE_1.merge(t_coordinateFactE_2)
    t_coordinateFactE_1.text=u"E"
    c_coordinateFactE_1 = table.cell(5,20)
    c_coordinateFactE_2 = table.cell(5,24)
    c_coordinateFactE_1.merge(c_coordinateFactE_2)
    c_coordinateFactE_1.text=row.getValue(u"经度")

    # 解译图斑信息
    t_interpretPolygon_1 = table.cell(6,0)
    t_interpretPolygon_2 = table.cell(6,24)
    t_interpretPolygon_1.merge(t_interpretPolygon_2)
    t_interpretPolygon_1.text=u"解译图斑信息"
    t_interpretPolygon_1.paragraphs[0].style="MyStyle"
    # title_style = document.styles.add_style('MyStyle',WD_STYLE_TYPE.PARAGRAPH)
    # title_style.font.size = Pt(14)
    # title_style.font.bold=True
    # t_interpretPolygon_1.add_paragraph(u"解译图斑信息",style="MyStyle")

    #图斑属性，图斑规模
    t_polygon_1 = table.cell(7,0)
    t_polygon_2 = table.cell(7,21)
    t_polygon_1.merge(t_polygon_2)
    t_polygon_1.text = u"图斑属性"
    t_polygon_1.paragraphs[0].style = "MyCenter"

    t_polygonSize_1 = table.cell(7,22)
    t_polygonSize_2 = table.cell(7,24)
    t_polygonSize_1.merge(t_polygonSize_2)
    t_polygonSize_1.text = u"图斑规模"
    t_polygonSize_1.paragraphs[0].style = "MyCenter"

    #矿山地质灾害
    t_mining_disaster_1 = table.cell(8,0)
    t_mining_disaster_2 = table.cell(9,6)
    t_mining_disaster_1.merge(t_mining_disaster_2)
    t_mining_disaster_1.text=u"①矿山地质灾害"
    #泥石流
    c_debrisflow_1 = table.cell(8,7)
    c_debrisflow_2 = table.cell(8,11)
    c_debrisflow_1.merge(c_debrisflow_2)
    c_debrisflow_1.text=u"□泥石流"
    #地裂缝
    c_geofracture_1 = table.cell(8,12)
    c_geofracture_2 = table.cell(8,16)
    c_geofracture_1.merge(c_geofracture_2)
    c_geofracture_1.text=u"□地裂缝"

    #地面塌陷
    c_surfaceCollapse_1 = table.cell(8,17)
    c_surfaceCollapse_2 = table.cell(8,21)
    c_surfaceCollapse_1.merge(c_surfaceCollapse_2)
    c_surfaceCollapse_1.text=u"□地面塌陷"

    #长轴
    t_majorAxis_1 = table.cell(8,22)
    t_majorAxis_2 = table.cell(8,24)
    t_majorAxis_1.merge(t_majorAxis_2)
    t_majorAxis_1.text=u"长轴（m）"

    #滑坡
    c_landslide_1 = table.cell(9,7)
    c_landslide_2 = table.cell(9,11)
    c_landslide_1.merge(c_landslide_2)
    c_landslide_1.text=u"□滑坡"

    #崩塌
    c_collapse_1 = table.cell(9,12)
    c_collapse_2 = table.cell(9,16)
    c_collapse_1.merge(c_collapse_2)
    c_collapse_1.text=u"□崩塌"

    #空白
    c_1 = table.cell(9,17)
    c_2 = table.cell(9,21)
    c_1.merge(c_2)

    #长轴内容
    c_majorAxis_1 = table.cell(9,22)
    c_majorAxis_2 = table.cell(9,24)
    c_majorAxis_1.merge(c_majorAxis_2)
    c_majorAxis_1.text=row.getValue(u"长轴")

    #矿山地质环境问题
    t_mining_env_1 = table.cell(10,0)
    t_mining_env_2 = table.cell(11,6)
    t_mining_env_1.merge(t_mining_env_2)
    t_mining_env_1.text=u"②矿山地质环境问题"
    #露天采场
    c_openMine_1 = table.cell(10,7)
    c_openMine_2 = table.cell(10,11)
    c_openMine_1.merge(c_openMine_2)
    c_openMine_1.text=u"□露天采场"
    #煤矸石堆
    c_coalWaste_1 = table.cell(10,12)
    c_coalWaste_2 = table.cell(10,16)
    c_coalWaste_1.merge(c_coalWaste_2)
    c_coalWaste_1.text=u"□矸石堆放"
    #尾矿库
    c_tailingPond_1 = table.cell(10,17)
    c_tailingPond_2 = table.cell(10,21)
    c_tailingPond_1.merge(c_tailingPond_2)
    c_tailingPond_1.text=u"□尾矿库"
    #短轴
    t_minorAxis_1 = table.cell(10,22)
    t_minorAxis_2 = table.cell(10,24)
    t_minorAxis_1.merge(t_minorAxis_2)
    t_minorAxis_1.text=u"短轴（m）"

    #工业广场
    c_industrialSquare_1 = table.cell(11,7)
    c_industrialSquare_2 = table.cell(11,11)
    c_industrialSquare_1.merge(c_industrialSquare_2)
    c_industrialSquare_1.text=u"□工业广场"
    #废石堆场
    c_rockWaste_1 = table.cell(11,12)
    c_rockWaste_2 = table.cell(11,16)
    c_rockWaste_1.merge(c_rockWaste_2)
    c_rockWaste_1.text=u"□废石堆场"
    #中转场地
    c_transitVenues_1 = table.cell(11,17)
    c_transitVenues_2 = table.cell(11,21)
    c_transitVenues_1.merge(c_transitVenues_2)
    c_transitVenues_1.text=u"□中转场地"

    mining_problem = row.getValue("Problem")
    if mining_problem==u"尾矿库":
        c_tailingPond_1.text=""
        p = c_tailingPond_1.paragraphs[0]
        r = p.add_run()
        r.font.name="Wingdings"
        r.add_text(u"")
        r_t = p.add_run(u"尾矿库")
    elif string.strip(mining_problem)==u"露天采场":
        c_openMine_1.text=""
        p = c_openMine_1.paragraphs[0]
        r = p.add_run()
        r.font.name = "Wingdings"
        r.add_text(u"")
        r_t = p.add_run(u"露天采场")
    elif string.strip(mining_problem)==u"工业广场":
        c_industrialSquare_1.text=""
        p = c_industrialSquare_1.paragraphs[0]
        r = p.add_run()
        r.font.name = "Wingdings"
        r.add_text(u"")
        r_t = p.add_run(u"工业广场")
    elif string.strip(mining_problem)==u"矸石堆放":
        c_coalWaste_1.text=""
        p = c_coalWaste_1.paragraphs[0]
        r = p.add_run()
        r.font.name = "Wingdings"
        r.add_text(u"")
        r_t = p.add_run(u"矸石堆放")
    elif string.strip(mining_problem)==u"废石堆场":
        c_rockWaste_1.text=""
        p = c_rockWaste_1.paragraphs[0]
        r = p.add_run()
        r.font.name = "Wingdings"
        r.add_text(u"")
        r_t = p.add_run(u"废石堆场")
    elif string.strip(mining_problem)==u"中转场地":
        c_transitVenues_1.text=""
        p = c_transitVenues_1.paragraphs[0]
        r = p.add_run()
        r.font.name = "Wingdings"
        r.add_text(u"")
        r_t = p.add_run(u"中转场地")

    #短轴内容
    c_minorAxis_1 = table.cell(11,22)
    c_minorAxis_2 = table.cell(11,24)
    c_minorAxis_1.merge(c_minorAxis_2)
    c_minorAxis_1.text=row.getValue(u"短轴")

    #③矿山地质环境治理
    t_miningEnvControl_1 = table.cell(12,0)
    t_miningEnvControl_2 = table.cell(13,6)
    t_miningEnvControl_1.merge(t_miningEnvControl_2)
    t_miningEnvControl_1.text=u"③矿山地质环境治理"
    # □土地复耕
    t_rehabilitation_1 = table.cell(12,7)
    t_rehabilitation_2 = table.cell(13,14)
    t_rehabilitation_1.merge(t_rehabilitation_2)
    t_rehabilitation_1.text=u"□土地复耕"
    # □植被绿化
    t_plantsGreening_1 = table.cell(12,15)
    t_plantsGreening_2 = table.cell(13,21)
    t_plantsGreening_1.merge(t_plantsGreening_2)
    t_plantsGreening_1.text=u"□植被绿化"
    #面积
    t_area_1 = table.cell(12,22)
    t_area_2 = table.cell(12,24)
    t_area_1.merge(t_area_2)
    t_area_1.text=u"面积(平米)"
    #面积内容
    c_area_1 = table.cell(13,22)
    c_area_2 = table.cell(13,24)
    c_area_1.merge(c_area_2)
    c_area_1.text=row.getValue(u"面积")

    #图斑影像
    t_polygonImage_1 = table.cell(14,0)
    t_polygonImage_2 = table.cell(14,4)
    t_polygonImage_1.merge(t_polygonImage_2)
    t_polygonImage_1.text=u"图斑影像"
    c_polygonImage_1 = table.cell(14,5)
    c_polygonImage_2 = table.cell(14,14)
    c_polygonImage_1.merge(c_polygonImage_2)
    pic_p = c_polygonImage_1.paragraphs[0]
    pic_r = pic_p.add_run()
    pic_r.add_picture(rsPicPath,width=Inches(2.3))

    #现场照片
    t_picture_1 = table.cell(14,15)
    t_picture_2 = table.cell(14,18)
    t_picture_1.merge(t_picture_2)
    t_picture_1.text=u"现场照片"
    c_picture_1 = table.cell(14,19)
    c_picture_2 = table.cell(14,24)
    c_picture_1.merge(c_picture_2)

    #遥感影像特征
    t_imgFeature_1 = table.cell(15,0)
    t_imgFeature_2 = table.cell(15,4)
    t_imgFeature_1.merge(t_imgFeature_2)
    t_imgFeature_1.text=u"遥感影像特征"
    c_imgFeature_1 = table.cell(15,5)
    c_imgFeature_2 = table.cell(15,24)
    c_imgFeature_1.merge(c_imgFeature_2)
    c_imgFeature_1.text=row.getValue(u"特征")

    #解译结果
    t_interpretationResult_1 = table.cell(16,0)
    t_interpretationResult_2 = table.cell(16,4)
    t_interpretationResult_1.merge(t_interpretationResult_2)
    t_interpretationResult_1.text=u"解译结果"
    c_interpretationResult_1 = table.cell(16,5)
    c_interpretationResult_2 = table.cell(16,8)
    c_interpretationResult_1.merge(c_interpretationResult_2)
    c_interpretationResult_1.text=row.getValue("Type")
    #解译人
    t_interpreter_1 = table.cell(16,9)
    t_interpreter_2 = table.cell(16,11)
    t_interpreter_1.merge(t_interpreter_2)
    t_interpreter_1.text=u"解译人"
    c_interpreter_1 = table.cell(16,12)
    c_interpreter_2 = table.cell(16,14)
    c_interpreter_1.merge(c_interpreter_2)
    c_interpreter_1.text=row.getValue(u"解译人")
    #解译时间
    t_interpretTime_1 = table.cell(16,15)
    t_interpretTime_2 = table.cell(16,18)
    t_interpretTime_1.merge(t_interpretTime_2)
    t_interpretTime_1.text=u"解译时间"
    c_interpretTime_1 = table.cell(16,19)
    c_interpretTime_2 = table.cell(16,24)
    c_interpretTime_1.merge(c_interpretTime_2)
    c_interpretTime_1.text=row.getValue(u"解译时间")
    #野外核查验证
    t_fieldVerify_1 = table.cell(17,0)
    t_fieldVerify_2 = table.cell(17,24)
    t_fieldVerify_1.merge(t_fieldVerify_2)
    t_fieldVerify_1.text=u"野外核查验证"
    t_fieldVerify_1.paragraphs[0].style = "MyStyle"

    #野外编号
    t_fieldCode_1 = table.cell(18,0)
    t_fieldCode_2 = table.cell(18,4)
    t_fieldCode_1.merge(t_fieldCode_2)
    t_fieldCode_1.text=u"野外编号"
    c_fieldCode_1 = table.cell(18,5)
    c_fieldCode_2 = table.cell(18,13)
    c_fieldCode_1.merge(c_fieldCode_2)
    c_fieldCode_1.text=row.getValue(u"野外编号")

    #照片编号
    t_photoCode_1 = table.cell(18, 14)
    t_photoCode_2 = table.cell(18, 19)
    t_photoCode_1.merge(t_photoCode_2)
    t_photoCode_1.text = u"照片编号"
    c_photoCode_1 = table.cell(18, 20)
    c_photoCode_2 = table.cell(18, 24)
    c_photoCode_1.merge(c_photoCode_2)
    c_photoCode_1.text = row.getValue(u"照片编号")

    #验证结果
    t_verifyResult_1 = table.cell(19,0)
    t_verifyResult_2 = table.cell(19,4)
    t_verifyResult_1.merge(t_verifyResult_2)
    t_verifyResult_1.text=u"野外验证结果"
    c_verifyResult_1 = table.cell(19,5)
    c_verifyResult_2 = table.cell(19,24)
    c_verifyResult_1.merge(c_verifyResult_2)
    c_verifyResult_1.text=row.getValue(u"验证结果")

    #核查情况
    t_verifyStatus_1 = table.cell(20,0)
    t_verifyStatus_2 = table.cell(20,4)
    t_verifyStatus_1.merge(t_verifyStatus_2)
    t_verifyStatus_1.text=u"核查情况"
    t_verified_1 = table.cell(20,5)
    t_verified_2 = table.cell(20,9)
    t_verified_1.merge(t_verified_2)
    t_verified_1.text=u"□核查"
    t_notVerified_1 = table.cell(20,10)
    t_notVerified_2 = table.cell(20,14)
    t_notVerified_1.merge(t_notVerified_2)
    t_notVerified_1.text=u"□未核查"
    t_verifier_1 = table.cell(20,15)
    t_verifier_2 = table.cell(20,18)
    t_verifier_1.merge(t_verifier_2)
    t_verifier_1.text=u"验证人"
    c_verifier_1 = table.cell(20,19)
    c_verifier_2 = table.cell(20,24)
    c_verifier_1.merge(c_verifier_2)
    c_verifier_1.text=row.getValue(u"验证人")

    #核查时间
    t_verifyTime_1 = table.cell(21,0)
    t_verifyTime_2 = table.cell(21,4)
    t_verifyTime_1.merge(t_verifyTime_2)
    t_verifyTime_1.text=u"核查时间"
    c_verifyTime_1 = table.cell(21,5)
    c_verifyTime_2 = table.cell(21,14)
    c_verifyTime_1.merge(c_verifyTime_2)
    c_verifyTime_1.text=row.getValue(u"核查时间")
    #审核人
    t_Auditor_1 = table.cell(21,15)
    t_Auditor_2 = table.cell(21,18)
    t_Auditor_1.merge(t_Auditor_2)
    t_Auditor_1.text=u"审核人"
    c_Auditor_1 = table.cell(21,19)
    c_Auditor_2 = table.cell(21,24)
    c_Auditor_1.merge(c_Auditor_2)
    c_Auditor_1.text=row.getValue(u"审核人")

#----------ArcTooolbox模式----------
docx_template_file = arcpy.GetParameterAsText(0)
table_title = arcpy.GetParameterAsText(1)
images_output_dir = arcpy.GetParameterAsText(2)
mxd = arcpy.mapping.MapDocument("CURRENT")

# #----------代码运行模式----------
# docx_template_file = "HenanProvince.docx"
# table_title = u"河南省矿山地质环境遥感解译信息表"
# images_output_dir = "h:\\tt\\tt-06-13"
# # mxd文件切记，不能用中文名，否则会报IO错
# mxd = arcpy.mapping.MapDocument(r"./TestExportJPG.mxd")

lyr = arcpy.mapping.ListLayers(mxd)[0]
df = arcpy.mapping.ListDataFrames(mxd)[0]

fields = arcpy.ListFields(lyr.dataSource)
fieldnames = [f.name for f in fields]

document = Document(docx_template_file)
cursor = arcpy.SearchCursor(lyr,fieldnames)

ii=0
for row in cursor:
    if ii<3:
        document.add_paragraph(table_title, "MyStyle")
        filename = row.getValue(u"解译编号")
        picPath= ExportToJPGFile(mxd,df,lyr,ii,images_output_dir,filename)
        CreateTable(document,row, picPath)
        print("finished the {0}th table...".format(ii+1))
        document.add_page_break()
    else:
        break
    ii = ii + 1
document.save(docx_template_file)
print("Congratulations!!! All tables finished.")

